package com.javaclimb.filmbuy.controller;


import cn.hutool.json.JSONObject;
import com.javaclimb.filmbuy.common.Result;
import com.javaclimb.filmbuy.service.AdminInfoService;
import com.javaclimb.filmbuy.service.OrderInfoService;
import com.javaclimb.filmbuy.service.UserInfoService;
import com.javaclimb.filmbuy.vo.EchartsData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 后台首页Controller
 */
@RestController
@RequestMapping("/echarts")
public class EchartsController {

    @Resource
    private AdminInfoService adminInfoService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private OrderInfoService orderInfoService;

    /**
     * 用户总数、总销售额、总销量
     */
    @GetMapping("/getTotal")
    public Result<Map<String,Object>> getTotal() {
        Map<String, Object> map = new HashMap<>();
        //用户总数
        map.put("totalUser",adminInfoService.count() + userInfoService.count());
        //总销售额
        map.put("totalPrice",orderInfoService.totalPrice());
        //总销量
        map.put("totalShopping",orderInfoService.totalShopping());
        return Result.success(map);
    }

    /*
     * 分类总销售额
     * 这个list包含两条数据，第一条是饼图需要的，第二条是柱状图需要的
     * */
    @GetMapping("/get/price")
    public Result<List<EchartsData>> price(){
        List<EchartsData> list = new ArrayList<>();
        Map<String, Double> typeMap = new HashMap<>();
        List<Map<String, Object>> typePriceList = orderInfoService.getTypePrice();
        for(Map<String, Object> map: typePriceList){
            typeMap.put((String) map.get("name"), (Double) map.get("price"));
        }
        getPieData("分类总销售额", list, typeMap);
        getBarData("分类总销售额", list, typeMap);
        return Result.success(list);
    }

    /*
     * 分类总销量
     * 这个list包含两条数据，第一条是饼图需要的，第二条是柱状图需要的
     * */
    @GetMapping("/get/shopping")
    public Result<List<EchartsData>> shopping(){
        List<EchartsData> list = new ArrayList<>();
        Map<String, Double> typeMap = new HashMap<>();
        List<Map<String, Object>> typePriceList = orderInfoService.getTypeCount();
        for(Map<String, Object> map: typePriceList){
            typeMap.put((String) map.get("name"), ((BigDecimal) map.get("count")).doubleValue());
        }
        getPieData("分类总销量", list, typeMap);
        getBarData("分类总销量", list, typeMap);
        return Result.success(list);
    }


    /*
     * 用dataMap填充饼图，放入pieList
     * @param name 饼图的标题
     * @param pieList 返回的
     * @param dataMap 要处理的map
     * */
    private void getPieData(String name, List<EchartsData> pieList, Map<String, Double> dataMap) {
        EchartsData pieData = new EchartsData();
        Map<String, String> titleMap = new HashMap<>();
        EchartsData.Series series = new EchartsData.Series();

        titleMap.put("text", name);
        pieData.setTitle(titleMap);

        series.setName(name + "比例");
        series.setType("pie");
        series.setRadius("55%");
        List<Object> objects = new ArrayList<>();
        for(String key: dataMap.keySet()){
            Double value = dataMap.get(key);
            objects.add(new JSONObject().putOpt("name", key).putOpt("value", value));
        }
        series.setData(objects);
        pieData.setSeries(Collections.singletonList(series));
        pieList.add(pieData); 
    }

    /*
     * 用dataMap填充柱状图，放入barList
     * @param name 柱状图的标题
     * @param barList 返回的
     * @param dataMap 要处理的map
     * */
    private void getBarData(String name, List<EchartsData> barList, Map<String, Double> dataMap) {
        EchartsData barData = new EchartsData();
        Map<String, String> titleMap = new HashMap<>();
        EchartsData.Series series = new EchartsData.Series();
        Map<String, Object> xAxisMap = new HashMap<>();

        List<Object> xAxisObjs = new ArrayList<>();
        List<Object> seriesObjs = new ArrayList<>();

        titleMap.put("text", name);
        barData.setTitle(titleMap);

        for(String key: dataMap.keySet()){
            xAxisObjs.add(key);
            seriesObjs.add(dataMap.get(key));
        }

        xAxisMap.put("data", xAxisObjs);
        barData.setxAxis(xAxisMap);

        barData.setyAxis(new HashMap<>());

        series.setData(seriesObjs);
        series.setType("bar");
        barData.setSeries(Collections.singletonList(series));

        barList.add(barData);
    }
}
